<?php
	/*************************/
	/**** Fonctions TIME *****/
	/*************************/

    function getAllTime() {
    	// la requête sql, on récupère tous les champs (*) de la table 'episode'
    	$sql = 'SELECT * from time';
    	return fetch($sql);
    }

    function getAllTimeOrdered() {
		$sql = 'SELECT `id_time`, `time`.`id_user`,`time`.`id_order`, `id_availability`, `id_categorie`, `date`, `duration` FROM `time` 
		INNER JOIN  `order` ON `time`.`id_order` = `order`.`id_order`
		ORDER BY order.date_order DESC';
		return fetch($sql);
    }
	
    function getTimeById($id) {
		$sql = 'SELECT * FROM time WHERE id="'.$id.'"';
		return fetch($sql);
    }
    
	function getTimeByUser($user){
		$sql = 'SELECT * FROM time WHERE user="'.$user.'"';
		return fetch($sql);
	}

	function getTimes($l = NULL){
		$limit = (int)$l;
		$sql ='SELECT user.id_user, user.firstname, user.lastname, user.mail, categorie.name, time.date, time.duration
		FROM time
		INNER JOIN user ON time.id_user = user.id_user
		INNER JOIN categorie ON time.id_categorie = categorie.id_categorie
		ORDER BY time.date DESC ';
		if($limit){
			$sql .= ' LIMIT 0, '.$limit.';';
		}
		return fetch($sql);
	}
	
	function insertTime($id_availability, $id_order, $id_user, $id_categorie, $datetime, $duration){
		$sql = 'INSERT INTO time (id_availability, id_order, id_user, id_categorie, date, duration) VALUES ('.$id_availability.', "'.$id_order.'", "'.$id_user.'", "'.$id_categorie.'", "'.$datetime.'", "'.$duration.'")';
		return mysql_query($sql);
	}

	function checkDates($newDate){
		$sql = 'SELECT id_time FROM time
		WHERE time.date = "'.$newDate.'"';
		return fetch($sql);
	}

	function getTimesByIdOrder($id_order){
		$sql = 'SELECT time.id_user, time.id_categorie, time.date, time.duration, time.id_availability FROM time
		WHERE time.id_order = '.$id_order.'';
		return fetch($sql);
	}
?>